package com.lijin.web;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lijin.converters.UserConverter;
import com.lijin.dataobject.dto.UserDto;
import com.lijin.dataobject.entity.User;
import com.lijin.dataobject.enums.SexEnum;
import com.lijin.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by gold-jx on 2017/6/2.
 */
@RestController
@Slf4j
@RequestMapping("/user")
@Api(description = "用户相关接口")
public class UserController {

    private final UserService userService;


    public UserController(UserService userService) {
        this.userService = userService;
    }


    @RequestMapping(value = "/register", method = RequestMethod.POST)
    @ApiOperation(value = "用户注册", tags = "User")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "tbName", value = "姓名", dataType = "String", required = true, paramType = "query"),
            @ApiImplicitParam(name = "tbAge", value = "年龄", dataType = "Integer", required = true, paramType = "query"),
            @ApiImplicitParam(name = "tbSex", value = "性别", dataType = "String", required = true, paramType = "query"),
            @ApiImplicitParam(name = "tbPassword", value = "密码", dataType = "String", required = true, paramType = "query")
    })
    public String createUser(
            @RequestParam(value = "tbName", required = true) String tbName,
            @RequestParam(value = "tbAge", required = false) Integer tbAge,
            @RequestParam(value = "tbSex", required = false) SexEnum tbSex,
            @RequestParam(value = "tbPassword", required = true) String tbPassword
    ) {



        User user = new User();
        user.setTbName(tbName);
        user.setTbAge(tbAge);
        user.setTbSex(tbSex.toString());
        user.setTbPassword(tbPassword);
        userService.register(user);
        return "SUCCESS";
    }


    @RequestMapping(value = "/deleteUser", method = RequestMethod.PUT)
    @ApiOperation(value = "删除用户", tags = "User")
    @ApiImplicitParam(name = "tbName", value = "姓名", dataType = "String", required = true, paramType = "query")
    public String deleteUser(
            @RequestParam(value = "tbName", required = true) String name
    ) {
        userService.deleteUser(name);
        return "SUCCESS";
    }


    @RequestMapping(value = "/updateUser", method = RequestMethod.POST)
    @ApiOperation(value = "用户修改", tags = "User")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "tbName", value = "姓名", dataType = "String", required = true, paramType = "query"),
            @ApiImplicitParam(name = "tbAge", value = "年龄", dataType = "Integer", required = true, paramType = "query"),
            @ApiImplicitParam(name = "tbSex", value = "性别", dataType = "String", required = true, paramType = "query"),
            @ApiImplicitParam(name = "tbPassword", value = "密码", dataType = "String", required = true, paramType = "query")
    })
    public String updateUser(
            @RequestParam(value = "tbName", required = true) String tbName,
            @RequestParam(value = "tbAge", required = false) Integer tbAge,
            @RequestParam(value = "tbSex", required = false) String tbSex,
            @RequestParam(value = "tbPassword", required = true) String tbPassword
    ) {
        User user = new User();
        user.setTbName(tbName);
        user.setTbAge(tbAge);
        user.setTbSex(tbSex);
        user.setTbPassword(tbPassword);
        userService.updateUser(user);
        return "SUCCESS";
    }


    @RequestMapping(value = "/findUserByName", method = RequestMethod.GET)
    @ApiOperation(value = "根据用户名查找用户", tags = "User")
    @ApiImplicitParam(name = "tbName", value = "姓名", dataType = "String", required = true, paramType = "query")
    public UserDto findUserByName(
            @RequestParam(value = "tbName", required = true) String name
    ) {
        User user = userService.findUserByName(name);
        UserConverter userConverter = new UserConverter();
        UserDto userDto = userConverter.convert(user);

        return userDto;
    }


    @RequestMapping(value = "/findAllUser", method = RequestMethod.GET)
    @ApiOperation(value = "查找所有用户", tags = "User")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "pageNum", value = "当前页", dataType = "Integer", required = false, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "页容量", dataType = "Integer", required = false, paramType = "query")
    })
    public PageInfo findAll(
            @RequestParam(value = "pageNum", required = false) Integer pageNum,
            @RequestParam(value = "pageSize", required = false) Integer pageSize
    ) {


        pageNum = pageNum == null ? 1 : pageNum;
        pageSize = pageSize == null ? 10 : pageSize;
        PageHelper.startPage(pageNum, pageSize);
        List<User> list =userService.findUserAll();
        PageInfo pageInfo = new PageInfo(list);
        Page page = (Page) list;

        return pageInfo;
    }

}
